package mest.controller.hank;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import mest.service.hank.PlanProductjobSaleoutService;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;

import mest.entity.plan_productjob_saleout_record;
import mest.service.hank.PlanProductjobSaleoutRecordService;
import mest.util.NumberConst;
import mest.util.ResultObj;
import mest.util.mestUtil;

/**
 * @Classname PlanProductjobSaleoutRecord
 * @Description TODO
 * @Date 2019/12/14 9:38
 * @Created by 18416
 * @Auther: henk
 */

@RestController
@RequestMapping("/PlanProductjobSaleoutRecord")
@CrossOrigin
public class PlanProductjobSaleoutRecord {

    @Resource
    private PlanProductjobSaleoutRecordService ppsors;
    @Resource
	private PlanProductjobSaleoutService ppss;

    /**
     * 添加 计划销售单详情,的同时添加   仓库单
     * @param json
     * @param request
     * @return
     */
    @RequestMapping("/inserList")
    public Object inserList(@RequestBody JSONObject json, HttpServletRequest request ){
        try {
        	//String theirDb = mestUtil.getCompanyDB(request);
            Map<String, Object> map = json.toJavaObject(Map.class);
            map.put("theirDb", mestUtil.getCompanyDB(request));
            List<plan_productjob_saleout_record> record = (List<plan_productjob_saleout_record>) map.get("list");
            //循环插入
            int i = ppsors.intsertList(record,mestUtil.getCompanyDB(request));
            //循环更新
			ppss.updateByone(mestUtil.getCompanyDB(request), record);
            //添加仓库单编码
            map.put("tableNumber", NumberConst.WareHouseSaleout_No + "-" + ppsors.getWarehouseSaleoutNumber(map));
            Object list = map.get("list");
            int wsId = ppsors.intsertWarehouseSaleout(map);
            //map.put("wsId", wsId);
            map.put("list", list);
            //System.out.println(map);
            int a = ppsors.insertWarehousePJSaleout(map);
            //System.out.println(map.toString());
            return ResultObj.createSuccessMsg(ResultObj.INSERT_SUCCESS);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			return ResultObj.createError();
		}
    }

    /**
     * 通过父级ID分页查询方法
     * @param index
     * @param pageSize
     * @param psId
     * @return
     */
    @RequestMapping("/queryAllModel")
    public ResultObj queryAllModel(int index,int pageSize,String psId, HttpServletRequest request){
		try {
			Map<String, Object> map = new HashMap<>();
			map.put("index", index-1);
			map.put("pageSize", pageSize);
			map.put("psId", psId);
			map.put("theirDb", mestUtil.getCompanyDB(request));
			List<plan_productjob_saleout_record> list = ppsors.selectAll(map);
			int count = ppsors.count(map);
			ResultObj resultObj = ResultObj.createSuccess();
			resultObj.setCount(count);
			resultObj.setData(list);
			return resultObj;
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			return ResultObj.createErrorMsg(ResultObj.ERROR2);
		}
	}
    
}
